package com.burgeon.backend.EnterpriseWeChat.rest.FAB;

import com.burgeon.backend.EnterpriseWeChat.entity.YCOrder;
import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class getProductByCode extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public getProductByCode(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public String getData(String code) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(code), url);
//        System.err.println(str);
        String code1 = new JSONArray(str).getJSONObject(0).getString("code");

        if (!"0".equals(code1)) {
            throw new Exception(new JSONArray(str).getJSONObject(0).getString("message"));
        }
        String count = new JSONArray(str).getJSONObject(0).getString("count");
        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");

//        ArrayList<FittingProduct> fittingProducts = new ArrayList<>();
       String productName = null;
        if(jsonArray.length()>0){
            productName = jsonArray.getJSONArray(0).getString(0);

        }

        return productName;
    }

    private MultiValueMap Data(String code) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段
        columns.add("'M_PRODUCT_ID;NAME'");//商品款号

        params.put("columns", columns);
        params.put("table", "M_V_PRODUCT_ALIAS");//表名
        params.put("count", "true");

        HashMap paramss = new HashMap();

        paramss.put("column","'NO'");//条码
        String a = "'"+code+"'";
        paramss.put("condition",a);

        params.put("params", paramss);
//        System.err.println(paramss);
        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        getProductByCode g = new getProductByCode("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(g.getData("6970891717100"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
